// pages/sign/sign.js
import {request, requestWrap} from "../../request/index.js"
Page({

    /**
     * 页面的初始数据
     */
    data: {
        devicetype: '',
        isHide: false,
        dataList: '',
        isShow: false,
        times: [],
        index: 0,
    },
    id: '',
    name: '',
    student: '',
    openid: '',
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        this.devicetype = getApp().globalData.devicetype
        this.openid = wx.getStorageSync('openid')
        this.student = wx.getStorageSync('student')
        this.getCourse()
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    },
    handleInput (e) {
      let name = e.currentTarget.dataset.name
      const {value} = e.detail
      this[name] = value
    },
    async getCourse () {
        let data = {}
        data.student_id = this.student.id
        if (this.name) {
            data.name = this.name
        }
        let res = requestWrap(await request({
            url:"api/wxapp/student/courseList",
            method:'GET',
            data
        }))
        if (res && res.code == 1) {
            if (res.data.is_pay != 1 && this.devicetype == 'ios') {
                this.setData({
                    isHide: true
                })
            } else {
                this.setData({
                    isHide: false
                })
            }
          if (res.data.list && res.data.list.length > 0) {
            let dataList = res.data.list.map(data => {
              let arr = []
              if (data.monday == 1) {
                arr.push('周一')
              }
              if (data.tuesday == 1) {
                arr.push('周二')
              }
              if (data.wednesday == 1) {
                arr.push('周三')
              }
              if (data.thursday == 1) {
                arr.push('周四')
              }
              if (data.friday == 1) {
                arr.push('周五')
              }
              if (data.saturday == 1) {
                arr.push('周六')
              }
              if (data.sunday == 1) {
                arr.push('周日')
              }
              data.school_week = arr
              return data
            })
            this.setData({
              dataList
            })
          } else {
            this.setData({
              dataList: ''
            })
          }
        }
    },
    btnFlag: true,
    isTimes: false,
    onSign (e) {
        // console.log(e)
        let data = e.currentTarget.dataset.record
        this.id = data.id
        if (data.is_times == 1) {
            this.isTimes = true
            let arr = []
            let num = parseInt(data.num)
            for (var i=1; i <= num; i++) {
                arr.push(i + '次')
            }
            // console.log(arr)
            this.setData({
                index: num - 1,
                isShow: true,
                times: arr
            })
        } else {
            this.isTimes = false
            this.signRequest()
        }
    },
    onChange (e) {
        this.setData({
            index: e.detail.value
        })
    },
    async signRequest () {
        let data = {}
        data.openid = this.openid
        data.student_id = this.student.id
        data.course_id = this.id
        if (this.isTimes) {
            data.num = parseInt(this.data.index) + 1
        }
        this.hidePopup()
        // console.log(data)
        // return
        this.btnFlag = false
        let res = requestWrap(await request({
            url:"api/wxapp/student/courseOrder",
            method:'GET',
            data
        }))
        // console.log(res)
        // return
        if (res && res.code == 1) {
            wx.requestPayment({
            timeStamp: res.data.time.toString(),
            nonceStr: res.data.nonceStr,
            package: res.data.package,
            signType: res.data.signType,
            paySign: res.data.paySign,
            success: function(res) {
                // console.log(res)
                wx.showToast({
                    title: '报名成功',
                    icon: 'none'
                })
            },
            fail: function(res) {
                // console.log(res)
                wx.showToast({
                title: res.msg,
                icon: 'none'
                })
            }
            })
        } else {
            wx.showToast({
            title: res.msg,
            icon: 'none'
            })
        }
        this.btnFlag = true
    },
    hidePopup () {
        this.setData({
            isShow: false
        })
    }
})